package cat.service.impl;

import cat.entity.Article;
import cat.entity.Category;
import cat.mapper.ArticleCategoryRefMapper;
import cat.mapper.ArticleMapper;
import cat.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    ArticleMapper articleMapper;

    @Resource
    ArticleCategoryRefMapper articleCategoryRefMapper;
    public List<Article> listRecentArticle(Integer n) {
        return articleMapper.listRecentArticle(n);
    }

    /*
    根据id查询
     */
    public Article selectArticleById(int articleId) {
        return articleMapper.selectArticleById(articleId);
    }

    /*
    分页查询
     */
    public PageInfo<Article> getPageArticleList(Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Article> articleList = articleMapper.findAll();
        //对每个文章，查出他对应的分类信息
        for (Article a : articleList) {
            List<Category> categoryList=articleCategoryRefMapper.listCategoryByArticleId(a.getArticleId());
            a.setCategoryList(categoryList);
        }
        return new PageInfo<Article>(articleList);
    }


    public void deleteArticleById(Integer articleId) {
        articleMapper.deleteArticleById(articleId);
    }
}
